<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="选择店铺：" prop="storeId">
        <el-select v-model="queryParams.storeId" placeholder="请选择店铺" filterable>
          <el-option v-for="(item,index) in storeList" :key="index"
                     :label="item.storeUserName + ' - ' + item.storeName "
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sellerStoreEmpowerList" @selection-change="handleSelectionChange" border
              stripe>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="店铺账号" align="center" prop="storeAccount"/>
      <el-table-column label="代运营账号" align="center" prop="agencyUserAccount"/>
      <el-table-column label="联系人" align="center" prop="contacts"/>
      <el-table-column label="联系电话" align="center" prop="contactInformation"/>
      <el-table-column label="主体" align="center" prop="storeBody"/>
      <el-table-column label="生效日期" align="center" prop="validityStart"/>
      <el-table-column label="失效日期" align="center" prop="validityEnd"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改卖家店铺授权对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="店铺账户" prop="storeId">
          <el-select v-model="form.storeId" placeholder="请选择店铺" filterable>
            <el-option v-for="(item,index) in storeList" :key="index"
                       :label="item.storeUserName + ' - ' + item.storeName "
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代运营账号" prop="agencyUserId">
          <el-select v-model="form.agencyUserId" placeholder="请选择运营人员账号" filterable>
            <el-option v-for="(item,index) in operateUserList" :key="index"
                       :label="item.nickName + ' - ' + item.userName "
                       :value="item.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺主体" prop="storeBody">
          <el-input v-model="form.storeBody" placeholder="请输入店铺主体"/>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入店铺联系人姓名"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInformation">
          <el-input v-model="form.contactInformation" placeholder="请输入店铺联系人电话号码"/>
        </el-form-item>
        <el-form-item label="有效期-开始日期" prop="validityStart">
          <el-date-picker
            v-model="form.validityStart"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期-结束日期" prop="validityEnd">
          <el-date-picker
            v-model="form.validityEnd"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addStore, deleteStore, editStore, getAgencyOperationsAssociation, queryStoreList} from "@/api/agencyOperation";
import {getStoreInfo} from "@/api/order/orderApi";
import {allocatedUserList} from "@/api/system/role";

export default {
  name: "SellerStoreEmpower",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卖家店铺授权表格数据
      sellerStoreEmpowerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeId: [{required: true, message: "店铺名称不能为空", trigger: "change"}],
        storeBody: [{required: true, message: "店铺主体不能为空", trigger: "blur"}],
        contacts: [{required: true, message: "店铺联系人不能为空", trigger: "blur"}],
        contactInformation: [{required: true, message: "店铺电话不能为空", trigger: "blur"}],
        validityStart: [{required: true, message: "请选择店铺生效时间", trigger: "change"}],
        validityEnd: [{required: true, message: "请选择店铺失效时间", trigger: "change"}],
        agencyUserId: [{required: true, message: "请选择运营人员账号", trigger: "change"}],
      },
      // 店铺id
      storeId: null,
      storeList: [],//店铺列表
      operateUserList: [],//运营人员列表
    };
  },
  created() {
    this.getList();
    this.getStoreList()
    this.getOperateUserList()
  },
  computed: {},
  methods: {
    //  获取店铺列表
    getStoreList() {
      getStoreInfo().then(response => {
        this.storeList = response
      })
    },
    // 获取运营人员列表
    getOperateUserList() {
      allocatedUserList({roleId: '1732328402797199362'}).then(response => {
          this.operateUserList = response.rows;
        }
      );
    },
    /** 查询卖家店铺授权列表 */
    getList() {
      this.loading = true;
      queryStoreList(this.queryParams).then(response => {
        this.sellerStoreEmpowerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeId: undefined,
        agencyUserId: undefined,
        storeBody: undefined,
        contacts: undefined,
        contactInformation: undefined,
        validityStart: undefined,
        validityEnd: undefined,
        storeUserName: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增店铺信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getAgencyOperationsAssociation(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log('this.form', this.form, this.$store.getters)
      for (let i = 0; i < this.storeList.length; i++) {
        if (this.storeList[i].id == this.form.storeId) {
          this.form.storeUserName = this.storeList[i].storeUserName
        }
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            editStore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除卖家店铺授权编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return deleteStore(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('seller/sellerStoreEmpower/export', {
        ...this.queryParams
      }, `sellerStoreEmpower_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>

<style lang="scss">
.empower-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;

  .center-box-item {
    display: flex;
    align-items: center;
    padding: 30px 0 20px 100px;

    &:last-child {
      padding-bottom: 50px;
    }
  }
}

.center-box {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 0 120px;
}

.type-box {
  display: flex;
  flex-wrap: wrap;

  .el-tag {
    margin: 5px;
  }
}

.type-text {
  display: flex;
  justify-content: center;
}
</style>
